﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Threading;
using Asistente.Model.Representation;
using System.Diagnostics;
using System.IO;

namespace Asistente
{
    /// <summary>
    /// Lógica de interacción para AppWindows.xaml
    /// </summary>
    public partial class AppWindows
    {
        readonly ViewModel viewModel;

        public AppWindows()
        {
            InitializeComponent();
            ObjectDataProvider provider = Resources["viewModel"] as ObjectDataProvider;
            viewModel = provider != null ? provider.ObjectInstance as ViewModel : null;

            //Dispatcher para backup automatico
            DispatcherTimer dispatcherTimer = new DispatcherTimer();
            dispatcherTimer.Tick += new EventHandler(DispatcherTimerTick);
            dispatcherTimer.Interval = new TimeSpan(0, 1, 0); //El backup se realizara cada un minuto
            dispatcherTimer.Start();
        }

        #region New

        private void NewPanelClick(object sender, RoutedEventArgs e)
        {
            var listBoxItem = TypeOfPanel.SelectedItem as ListBoxItem;
            if (listBoxItem != null)
            {
                String type = listBoxItem.Content.ToString();
                viewModel.NewPanel(type);

                if (type.Equals("Graph"))
                {
                    GoToGraphTab();
                }
                else
                {
                    if (type.Equals("Report"))
                    {
                        GoToReadOnlyPanel();
                    }
                    else
                    {
                        GoToEditablePanel();
                    }
                }
            }
            Refresh(AppTreeView);
        }

        private void NewEntityClick(object sender, RoutedEventArgs e)
        {
            viewModel.NewEntity();
            GoToEntityTab();
            Refresh(EntitiesBox);
        }

        private void CloneUseCaseClick(object sender, RoutedEventArgs e)
        {
            viewModel.CloneUseCase();
            Refresh(AppTreeView);
        }
        

        private void NewControlReadOnlyPanelClick(object sender, RoutedEventArgs e)
        {
            viewModel.NewControlReadOnlyPanel();
            Refresh(ControlsReadOnly); 
            Refresh(AppTreeView);
        }

        private void NewActionClick(object sender, RoutedEventArgs e)
        {
            viewModel.NewAction();
            Refresh(actionsGrid);
        }

        private void NewUseCaseActionClick(object sender, RoutedEventArgs e)
        {
            viewModel.NewUseCaseAction((UseCase)ComboBoxUseCase.SelectedItem);
            Refresh(actionsGrid);
        }

        private void NewMasterDetailClick(object sender, RoutedEventArgs e)
        {
            viewModel.NewMasterDetail();
            GoToMasterDetail();
            Refresh(AppTreeView);
        }

        private void NewControlEditablePanelClick(object sender, RoutedEventArgs e)
        {
            viewModel.NewControlEditablePanel();
            Refresh(ControlsEditable);
            Refresh(AppTreeView);
        }

        private void NewScreenClick(object sender, RoutedEventArgs e)
        {
            viewModel.NewScreen();
            GoToScreenTab();
            Refresh(AppTreeView);
        }

        private void NewUseCaseClick(object sender, RoutedEventArgs e)
        {
            UseCasePattern pattern = (UseCasePattern)UseCasePatternBoxApp.SelectedItem;
            Entity entity = (Entity)EntitiesBox.SelectedItem;
            if (UseCaseLayoutCombinationBoxApp.SelectedItem != null)
            {
                UseCaseLayoutCombinations layout = (UseCaseLayoutCombinations)UseCaseLayoutCombinationBoxApp.SelectedItem;
                viewModel.NewUseCase(entity, pattern, layout);
                UseCaseLayoutCombinationBoxApp.SelectedItem = layout;
            }

            else
            {
                viewModel.NewUseCase(entity, pattern);
            }

            UseCasePatternBoxUC.SelectedItem = pattern;
            Refresh(AppTreeView);
            Refresh(ComboBoxUseCase);
            GoToUSeCaseTab();


        }

        private void NewMasterClick(object sender, RoutedEventArgs e)
        {
            var listBoxItem = TypeOfPanelMasterDetail.SelectedItem as ListBoxItem;
            if (listBoxItem != null)
            {
                String type = listBoxItem.Content.ToString();
                viewModel.NewMaster(type);
                GoToEditablePanel();
                Refresh(AppTreeView);
            }
        }

        private void NewDetailClick(object sender, RoutedEventArgs e)
        {
            var listBoxItem = TypeOfPanelMasterDetail.SelectedItem as ListBoxItem;
            if (listBoxItem != null)
            {
                String type = listBoxItem.Content.ToString();
                viewModel.NewDetail(type);
                GoToEditablePanel();
                Refresh(AppTreeView);
            }
        }
        #endregion

        #region Click

        private void PlayStartClick(object sender, RoutedEventArgs e)
        {
            viewModel.PlayPasue();
            Refresh(PlayPauseButton);
            
        }

        private void UpdateIfpugClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            viewModel.UpdateIfpug();
            IfpugModel.Text = viewModel.IfpugRepresentation.ToString();
            GoToSummaryTab();
        }

        private void UpdateCosmicClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            viewModel.UpdateCosmic();
            CosmicModel.Text = viewModel.CosmicRepresentation.ToString();
            GoToSummaryTab();
        }

        private void UpdateClick(object sender, RoutedEventArgs e)
        {
            viewModel.Update();
            IfpugModel.Text = viewModel.IfpugRepresentation.ToString();
            CosmicModel.Text = viewModel.CosmicRepresentation.ToString();
            GoToSummaryTab();
        }

        private void ImportClick(object sender, RoutedEventArgs e)
        {
            viewModel.Import();
        }

        private void ExportClick(object sender, RoutedEventArgs e)
        {
            viewModel.Export();
        }

        private void ChangePatternClick(object sender, RoutedEventArgs e)
        {
            // Display message box
            MessageBoxResult result = MessageBox.Show("Do you really want to change this pattern? This pattern will be deleted.", "Message", MessageBoxButton.YesNo, MessageBoxImage.Warning);

            switch (result)
            {
                case MessageBoxResult.Yes: // User pressed Yes button                   
                    
                    UseCasePattern pattern = (UseCasePattern)UseCasePatternBoxUC.SelectedItem;
                    UseCase uc = viewModel.SelectedUseCase;
                    viewModel.ChangeUseCase(uc, pattern);
                    UseCasePatternBoxUC.SelectedItem = pattern;
                    Refresh(AppTreeView);
                    break;
                case MessageBoxResult.No:// User pressed No button                    
                    break;
            }
        }

        private void AsociateEntitiesClick(object sender, RoutedEventArgs e)
        {
            List<Entity> entities = MultipleEntitySelection.ShowModal(viewModel.App);
            if (entities != null)
            {
                viewModel.SelectedControl.DataGroup = entities.Count != 0 ? new Query(entities) : null;
            }
            Refresh(EditablePanel);
            Refresh(ControlsReadOnly);
        }

        private void ReferenceEntityClick(object sender, RoutedEventArgs e)
        {
            List<Entity> entities = MultipleEntitySelection.ShowModal(viewModel.App);
            if (entities != null)
            {
                viewModel.SelectedEditablePanel.UpdatesDeletesCascade = entities;
                Refresh(referencedEntitiesLabel);
            }
        }

        private void DefineEntityClick(object sender, RoutedEventArgs e)
        {
            Entity entity = SingleEntitySelection.ShowModal(viewModel.App);
            if (entity != null)
            {
                viewModel.SelectedEditablePanel.Entity = entity;
                Refresh(labelSelectedEntity);
            }
        }

        private void DefineDataGroupClick(object sender, RoutedEventArgs e)
        {
            List<Entity> entities = MultipleEntitySelection.ShowModal(viewModel.App);
            if (entities != null)
            {
                viewModel.SelectedReadOnlyPanel.DataGroup = new Query(entities);
            }
            Refresh(dataGroupLabelReadOnlyPanel);
            Refresh(dataGroupLabelGraph);
        }

        #endregion

        #region Delete

        private void DeleteEntityClick(object sender, RoutedEventArgs e)
        {
            // Display message box
            MessageBoxResult result = MessageBox.Show("Do you really want to delete this entity?", "Message", MessageBoxButton.YesNo, MessageBoxImage.Error);
            if (viewModel.CanRemoveEntity(viewModel.SelectedEntity))
            {
                switch (result)
                {
                    case MessageBoxResult.Yes:
                        // User pressed Yes button
                        viewModel.DeleteEntity();
                        Refresh(EntitiesBox);
                        break;
                    case MessageBoxResult.No:
                        // User pressed No button
                        break;
                }
            }
            else
            {
                MessageBox.Show("the entity can't be removed because it's being used by another component");
            }
        }

        private void DeleteApplicationClick(object sender, RoutedEventArgs e)
        {
            // Display message box
            MessageBoxResult result = MessageBox.Show("Do you really want to delete this app?", "Message", MessageBoxButton.YesNo, MessageBoxImage.Error);

            switch (result)
            {
                case MessageBoxResult.Yes:
                    // User pressed Yes button
                    viewModel.Reset();
                    break;
                case MessageBoxResult.No:
                    // User pressed No button
                    break;
            }
        }

        private void DeleteSelectedEntityClick(object sender, RoutedEventArgs e)
        {
            viewModel.DeleteSelectedEntity();
            Refresh(labelSelectedEntity);
        }

        private void DeleteMasterDetailClick(object sender, RoutedEventArgs e)
        {
            // Display message box
            MessageBoxResult result = MessageBox.Show("Do you really want to delete this master detail?", "Message", MessageBoxButton.YesNo, MessageBoxImage.Error);

            switch (result)
            {
                case MessageBoxResult.Yes:
                    // User pressed Yes button
                    viewModel.DeleteMasterDetail();
                    GoToApplicationTab();
                    Refresh(AppTreeView);
                    break;
                case MessageBoxResult.No:
                    // User pressed No button
                    break;
            }
        }

        private void DeleteUseCaseClick(object sender, RoutedEventArgs e)
        {
            // Display message box
            MessageBoxResult result = MessageBox.Show("Do you really want to delete this use case?", "Message", MessageBoxButton.YesNo, MessageBoxImage.Error);

            switch (result)
            {
                case MessageBoxResult.Yes:
                    // User pressed Yes button
                    viewModel.DeleteUseCase();
                    GoToApplicationTab();
                    Refresh(AppTreeView);
                    break;
                case MessageBoxResult.No:
                    // User pressed No button
                    break;
            }
        }

        private void DeleteReadOnlyPanelClick(object sender, RoutedEventArgs e)
        {
            // Display message box
            MessageBoxResult result = MessageBox.Show("Do you really want to change this readonly panel?", "Message", MessageBoxButton.YesNo, MessageBoxImage.Warning);

            switch (result)
            {
                case MessageBoxResult.Yes: // User pressed Yes button                   
                    viewModel.DeleteReadOnlyPanel();
                    GoToApplicationTab();
                    Refresh(AppTreeView);
                    break;
                case MessageBoxResult.No:// User pressed No button                    
                    break;
            }
        }

        private void DeleteEditablePanelClick(object sender, RoutedEventArgs e)
        {
            // Display message box
            MessageBoxResult result = MessageBox.Show("Do you really want to change this editable panel?", "Message", MessageBoxButton.YesNo, MessageBoxImage.Warning);

            switch (result)
            {
                case MessageBoxResult.Yes: // User pressed Yes button                   
                    viewModel.DeleteEditablePanel();
                    GoToApplicationTab();
                    Refresh(AppTreeView);
                    break;
                case MessageBoxResult.No:// User pressed No button                    
                    break;
            }
        }

        private void DeleteScreenClick(object sender, RoutedEventArgs e)
        {
            // Display message box
            MessageBoxResult result = MessageBox.Show("Do you really want to change this screen?", "Message", MessageBoxButton.YesNo, MessageBoxImage.Warning);

            switch (result)
            {
                case MessageBoxResult.Yes: // User pressed Yes button                   
                    viewModel.DeleteScreen();
                    GoToApplicationTab();
                    Refresh(AppTreeView);
                    break;
                case MessageBoxResult.No:// User pressed No button                    
                    break;
            }
        }

        private void DeleteEditablePanelControlButton_Click(object sender, RoutedEventArgs e)
        {
            viewModel.SelectedEditablePanel.Controls.Remove(viewModel.SelectedControl);
            Refresh(ControlsEditable);
            Refresh(AppTreeView);
        }

        private void DeleteReadOnlyPanelControl_Click(object sender, RoutedEventArgs e)
        {
            viewModel.SelectedReadOnlyPanel.Controls.Remove(viewModel.SelectedControl);
            Refresh(ControlsReadOnly);
            Refresh(AppTreeView);
        }

        private void DeleteAction_Click(object sender, RoutedEventArgs e)
        {
            Model.Representation.Action selectedAction = actionsGrid.SelectedItem as Model.Representation.Action;
            viewModel.SelectedScreen.Actions.Remove(selectedAction);
            Refresh(actionsGrid);

        }

        #endregion
        /// <summary>
        /// Si se hace click sobre el arbol de aplicacion sin cambiar la seleccion se vuelve a actualizar el panel secundario
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void TreeViewGotFocus(object sender, RoutedEventArgs e)
        {
            AppTreeViewSelectedItemChanged(sender, null);
        } 

        #region SelectionChanged

        /// <summary>
        /// Seleccionamos un tab dependiendo del tipo de objeto seleccionado enel tree view. Cambiamos de 
        /// tab cuando cambia la seleccion del tree view.
        /// </summary>
        private void AppTreeViewSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if (AppTreeView.SelectedItem != null)
            {
                if (AppTreeView.SelectedItem.GetType() == typeof(UseCase))
                {
                    GoToUSeCaseTab();
                    viewModel.SelectedUseCase = AppTreeView.SelectedItem as UseCase;
                }
                else
                {
                    if (AppTreeView.SelectedItem.GetType() == typeof(Screen))
                    {
                        GoToScreenTab();
                        viewModel.SelectedScreen = AppTreeView.SelectedItem as Screen;
                    }
                    else
                    {
                        if (AppTreeView.SelectedItem.GetType() == typeof(MasterDetail))
                        {
                            viewModel.SelectedMasterDetail = AppTreeView.SelectedItem as MasterDetail;
                            GoToMasterDetail();
                        }
                        else
                        {
                            //Si es un panel
                            if (typeof(Model.Representation.Panel).IsAssignableFrom(AppTreeView.SelectedItem.GetType()))
                            {
                                //Si es editable
                                if (typeof(EditablePanel).IsAssignableFrom(AppTreeView.SelectedItem.GetType()))
                                {
                                    viewModel.SelectedEditablePanel = AppTreeView.SelectedItem as EditablePanel;
                                    GoToEditablePanel();
                                }
                                else //Es un ReadOnlyPanel
                                {
                                    if (AppTreeView.SelectedItem.GetType() == typeof(Graph))
                                    {
                                        viewModel.SelectedGraph = AppTreeView.SelectedItem as Graph;
                                        GoToGraphTab(); // Es una grafica, tiene un panel aparte
                                    }
                                    else
                                    {
                                        viewModel.SelectedReadOnlyPanel = AppTreeView.SelectedItem as ReadOnlyPanel;
                                        GoToReadOnlyPanel(); // Es un reporte
                                    }
                                }
                            }
                            else if (AppTreeView.SelectedItem.GetType() == typeof(Asistente.Model.Representation.Control)) { 
                                
                            }
                            else // Es la raiz del arbol Application
                            {
                                GoToApplicationTab();
                            }
                        }
                    }
                }
            }
        }

        private void ControlGridEditablePanelSelection(object sender, SelectionChangedEventArgs e)
        {
            viewModel.SelectedControl = ControlsEditable.SelectedItem as Model.Representation.Control;
        }

        private void ControlGridReadOnlyPanelSelection(object sender, SelectionChangedEventArgs e)
        {
            viewModel.SelectedControl = ControlsReadOnly.SelectedItem as Model.Representation.Control;
        }

        private void EntitiesBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            viewModel.SelectedEntity = EntitiesBox.SelectedItem as Entity;
            GoToEntityTab();
        }

        private void TypeOfPanelSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var listBoxItem = TypeOfPanel.SelectedItem as ListBoxItem;
            if (listBoxItem != null)
                viewModel.TypeOfPanel = listBoxItem.ToString();
        }

        private void UseCasePatternBoxAppSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            viewModel.UseCasePattern = (UseCasePattern)UseCasePatternBoxApp.SelectedItem;// para binding de que el selected item sea lo que hay en viewModel.UseCasePattern
            viewModel.SelectedUseCasePattern = true;
        }

        private void UseCaseLayoutCombinationBoxAppSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            viewModel.UseCaseLayoutCombination = (UseCaseLayoutCombinations)UseCaseLayoutCombinationBoxApp.SelectedItem;
            viewModel.SelectedUseCaseLayout = true;
        }

        private void UseCasePatternBoxUcSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            viewModel.UseCasePattern = (UseCasePattern)UseCasePatternBoxUC.SelectedItem;
            // para binding de que el selected item sea lo que hay en viewModel.UseCasePattern
            viewModel.SelectedUseCasePattern = true;
        }
        private void masterDetails_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            viewModel.SelectedMasterDetail = masterDetails.SelectedItem as MasterDetail;
            GoToMasterDetail();
        }

        private void panels_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            viewModel.SelectedEditablePanel = panels.SelectedItem as EditablePanel;
            GoToEditablePanel();
        } 
        #endregion



        //TODO : Como actualizar controles sin hacer esto
        private void Refresh(FrameworkElement element)
        {

            var currentNode = this.AppTreeView.SelectedItem as Element;

                var dc = element.DataContext;
                element.DataContext = null;
                element.DataContext = dc;

   
                if (currentNode != null)
                {
                    List<Element> hierarchicalItems=this.viewModel.App.getHierarchicalHigherData(currentNode);
                    foreach (Element item in hierarchicalItems)
                    {
                        item.IsExpanded = false;
                    }
                    foreach (Element item in hierarchicalItems)
                    {
                        item.IsExpanded = true;
                    }
                    currentNode.IsSelected = true;
                }
        }
        
        //Un metodo para cada uno de los tabs, segun el nombre mapeamos a el indice para evitar escribir
        //n veces cada indice en el codigo.

        #region Goto

        private void GoToApplicationTab()
        {
            Tab.SelectedIndex = 0;
        }

        private void GoToUSeCaseTab()
        {
            Tab.SelectedIndex = 1;
        }

        private void GoToScreenTab()
        {
            Tab.SelectedIndex = 2;
        }

        private void GoToEditablePanel()
        {
            Tab.SelectedIndex = 3;
        }

        private void GoToReadOnlyPanel()
        {
            Tab.SelectedIndex = 4;
        }

        private void GoToGraphTab()
        {
            Tab.SelectedIndex = 5;
        }

        private void GoToMasterDetail()
        {
            Tab.SelectedIndex = 6;
        }

        private void GoToEntityTab()
        {
            Tab.SelectedIndex = 7;
        }

        private void GoToSummaryTab()
        {
            Tab.SelectedIndex = 8;
        }

        #endregion

        #region Question calls

        private void QuestionTabApplicationButtonClick(object sender, RoutedEventArgs e)
        {
            //aca hay que abrir el archivo Help/Index.htm
            DirectoryInfo b = Directory.GetParent(Directory.GetCurrentDirectory());
            //b = Directory.GetParent(b.ToString());
            try
            {
                String[] a = Directory.GetFiles(b + "", "Ayuda Asistente COSIF.CHM", SearchOption.AllDirectories);
                Process.Start(a[0]);
            }
            catch (Exception ) {
                MessageBox.Show("An error occurred when attempting to open the file Ayuda Asistente COSIF.CHM" + b);
           }
        }

        private void QuestionTabUseCaseButtonClick(object sender, RoutedEventArgs e)
        {
           DirectoryInfo b = Directory.GetParent(Directory.GetCurrentDirectory());
            //b = Directory.GetParent(b.ToString());
            try
            {
                String[] a = Directory.GetFiles(b + "", "Ayuda Asistente COSIF.CHM", SearchOption.AllDirectories);
                Process.Start(a[0]);
            }
            catch (Exception ) {
                MessageBox.Show("An error occurred when attempting to open the file Ayuda Asistente COSIF.CHM");
            }
        }

        private void QuestionTabScreenButtonClick(object sender, RoutedEventArgs e)
        {
             DirectoryInfo b = Directory.GetParent(Directory.GetCurrentDirectory());
            //b = Directory.GetParent(b.ToString());
            try
            {
                String[] a = Directory.GetFiles(b + "", "Ayuda Asistente COSIF.CHM", SearchOption.AllDirectories);
                Process.Start(a[0]);
            }
            catch (Exception ) {
               MessageBox.Show("An error occurred when attempting to open the file Ayuda Asistente COSIF.CHM");
            }
        }

        private void QuestionTabEditablePanelButtonClick(object sender, RoutedEventArgs e)
        {
            DirectoryInfo b = Directory.GetParent(Directory.GetCurrentDirectory());
           // b = Directory.GetParent(b.ToString());
            try
            {
                String[] a = Directory.GetFiles(b + "", "Ayuda Asistente COSIF.CHM", SearchOption.AllDirectories);
                Process.Start(a[0]);
            }
            catch (Exception)
            {
                MessageBox.Show("An error occurred when attempting to open the file Ayuda Asistente COSIF.CHM");
            }
        }

        private void QuestionTabReadOnlyPanelButtonClick(object sender, RoutedEventArgs e)
        {
            DirectoryInfo b = Directory.GetParent(Directory.GetCurrentDirectory());
            //b = Directory.GetParent(b.ToString());
            try
            {
                String[] a = Directory.GetFiles(b + "", "Ayuda Asistente COSIF.CHM", SearchOption.AllDirectories);
                Process.Start(a[0]);
            }
            catch (Exception)
            {
                MessageBox.Show("An error occurred when attempting to open the file aneles de Ayuda Asistente COSIF.CHM");
            }
        }

        private void QuestionTabEntityButtonClick(object sender, RoutedEventArgs e)
        {
            DirectoryInfo b = Directory.GetParent(Directory.GetCurrentDirectory());
           // b = Directory.GetParent(b.ToString());
            try
            {
                String[] a = Directory.GetFiles(b + "", "Ayuda Asistente COSIF.CHM", SearchOption.AllDirectories);
                Process.Start(a[0]);
            }
            catch (Exception)
            {
                MessageBox.Show("An error occurred when attempting to open the file Ayuda Asistente COSIF.CHM");
            }
        }

        private void QuestionTabGraphButtonClick(object sender, RoutedEventArgs e)
        {
             DirectoryInfo b = Directory.GetParent(Directory.GetCurrentDirectory());
            //b = Directory.GetParent(b.ToString());
            try
            {
                String[] a = Directory.GetFiles(b + "", "Ayuda Asistente COSIF.CHM", SearchOption.AllDirectories);
                Process.Start(a[0]);
            }
            catch (Exception ) {
                MessageBox.Show("An error occurred when attempting to open the file Ayuda Asistente COSIF.CHM");
            }
        }

        private void QuestionTabMasterDetailsButtonClick(object sender, RoutedEventArgs e)
        {
             DirectoryInfo b = Directory.GetParent(Directory.GetCurrentDirectory());
           // b = Directory.GetParent(b.ToString());
            try
            {
                String[] a = Directory.GetFiles(b + "", "Ayuda Asistente COSIF.CHM", SearchOption.AllDirectories);
                Process.Start(a[0]);
            }
            catch (Exception ) {
                MessageBox.Show("An error occurred when attempting to open the file Ayuda Asistente COSIF.CHM");
            }
        }

        private void QuestionTabDebuggingButtonClick(object sender, RoutedEventArgs e)
        {
            DirectoryInfo b = Directory.GetParent(Directory.GetCurrentDirectory());
           // b = Directory.GetParent(b.ToString());
            try
            {
                String[] a = Directory.GetFiles(b + "", "Ayuda Asistente COSIF.CHM", SearchOption.AllDirectories);
                Process.Start(a[0]);
            }
            catch (Exception)
            {
               MessageBox.Show("An error occurred when attempting to open the file Ayuda Asistente COSIF.CHM");
            }
        }

        #endregion

        private void ExportHtmlIfpugResult(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            viewModel.ExportHtmlIfpugResult();
        }
        private void ExportCosmicSummary(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            viewModel.ExportCosmicSummary();
        }

        private void DispatcherTimerTick(object sender, EventArgs e)
        {
            
            const string path = @"C:\Cosif\";

            // Si no existe el directorio de respaldos se crea uno
            if (!System.IO.Directory.Exists(path))
            {
                System.IO.Directory.CreateDirectory(path);
            }
            viewModel.UpdateAppTime();
            viewModel.Export(path + "Backup_" +
                DateTime.Now.Year.ToString() +
                DateTime.Now.Month.ToString() +
                DateTime.Now.Day.ToString() +
                "_" + DateTime.Now.Hour.ToString() +
                "." + DateTime.Now.Minute.ToString() +
                "." + DateTime.Now.Second.ToString() + ".xml");
        }

        

          
    }
}
